package com.gugee.common.web.system.promise.validate;

import com.gugee.common.web.system.promise.repository.PermissionLimitRepository;
import com.gugee.common.web.system.user.repository.UserRepository;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CountLimit extends Validate{

    @Autowired
    UserRepository userRepository;
    @Autowired
    PermissionLimitRepository permissionLimitRepository;

    @Override
    public Boolean validate(long roleId, Object... params) {
        val permissionLimitOptional = permissionLimitRepository.findByPermissionIdAndProductId(super.permissionId, roleId);
        if(permissionLimitOptional.isPresent()){
            if(Integer.parseInt(params[0].toString()) < permissionLimitOptional.get().getLimitCount()){
                return true;
            }
            super.tips = permissionLimitOptional.get().getTips();
        }
        return false;
    }
}
